from django.db import models

from user.models import User
# Create your models here.
from utils.common_base_models import BaseModel


class Project(BaseModel):
    """
    产品
    """
    title = models.CharField(max_length=16, unique=True, verbose_name='名称')
    price = models.DecimalField(max_digits=7, decimal_places=2, verbose_name='价格', default=19999.99)
    renovation_time = models.CharField(max_length=16, verbose_name='装修时间', help_text='以天为单位')
    renovation_range = models.CharField(max_length=16, verbose_name='装修范围', default='全国')
    sales_volume = models.BigIntegerField(verbose_name='销量', default=1000)
    desc = models.TextField(verbose_name='商品简介', null=True, blank=True)

    product_parameters = models.CharField(max_length=225, verbose_name='产品参数', help_text='包多少平米')
    after_sales = models.CharField(max_length=225, verbose_name='售后保证', help_text='保证多少年', default='10年')
    home_img = models.ImageField(upload_to='project_img', verbose_name='首页图片')
    watch_num = models.BigIntegerField(verbose_name='观看数量', default=865)
    # 优化表中只存储点赞数量，记录在另一个表中
    comment_num = models.BigIntegerField(verbose_name='评论数量', default=366)
    style = models.CharField(max_length=16, verbose_name='风格', default='简单大方')
    # is_shoucang = models.BooleanField(verbose_name='收藏', default=False)


    # 用户评价 外键

    class Meta:
        db_table = 'youzhu_Project'  # 指定表名
        verbose_name = '产品'  # 后台管理看到的中文
        verbose_name_plural = verbose_name

    def __str__(self):  # 打印对象，显示的
        return self.title


class ProjectDetailImg(BaseModel):
    """
    产品详情页轮播图
    """
    image = models.ImageField(upload_to='project_img', verbose_name='图片')
    qiniu_link = models.CharField(max_length=255, verbose_name='七牛云链接', null=True, blank=True)
    home = models.ForeignKey(to='Project', on_delete=models.CASCADE, null=True, blank=True,
                             verbose_name='产品', related_name='p_p')

    class Meta:
        db_table = 'youzhu_ProjectDetailImg'
        verbose_name_plural = '产品详情页轮播图'


class ProjectComment(BaseModel):
    """评论表"""
    project = models.ForeignKey(to='Project', on_delete=models.CASCADE, null=True, blank=True,
                                verbose_name='产品', related_name='pc_p')

    comment = models.TextField(verbose_name='评论内容')

    userinfo = models.ForeignKey(verbose_name='用户', to=User, on_delete=models.DO_NOTHING, db_constraint=False,
                                 null=True, blank=True, related_name='pro_comment_userinfo')

    reply = models.ForeignKey(verbose_name='父评论', to='self', on_delete=models.DO_NOTHING, db_constraint=False,
                              null=True, blank=True, related_name='pro_comment_reply')

    depth = models.PositiveIntegerField(verbose_name='评论深度', default=1)

    # root = models.PositiveIntegerField(verbose_name='根id', null=True, blank=True)
    class Meta:
        db_table = 'youzhu_ProjectComment'
        verbose_name_plural = '评论表'

    def __str__(self):
        return self.project.title

class Shoucang(BaseModel):
    """
    收藏
    """
    is_shoucang = models.BooleanField(verbose_name='收藏', default=False)
    userinfo = models.ForeignKey(verbose_name='用户', to=User, on_delete=models.DO_NOTHING, db_constraint=False,
                                 null=True, blank=True, related_name='shoucang_comment_userinfo')
    project = models.ForeignKey(verbose_name='产品', to=Project, on_delete=models.DO_NOTHING, db_constraint=False,
                                 null=True, blank=True, related_name='shoucang_project')
    class Meta:
        db_table = 'youzhu_Shoucang'
        verbose_name_plural = '评论表'
class ProjectUp(BaseModel):
    """点赞"""
    is_upvote = models.BooleanField(verbose_name='收藏', default=False)
    userinfo = models.ForeignKey(verbose_name='用户', to=User, on_delete=models.DO_NOTHING, db_constraint=False,
                                 null=True, blank=True, related_name='ProjectUp_userinfo')

    project = models.ForeignKey(to='Project', on_delete=models.CASCADE, null=True, blank=True,
                               verbose_name='产品', related_name='pu_pj',db_constraint=False)

    class Meta:
        db_table = 'youzhu_ProjectUp'
        verbose_name_plural = '产品点赞表'
